/**
 * Copyright (c) Jan 15, 2013 8:28:03 PM WKM and/or its affiliates. 
 * Use is subject to license terms.
 * All rights reserved.
 */
package org.extblog.wkm.web.dao.face;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.extblog.wkm.web.dao.AbstractDao;
import org.extblog.wkm.web.util.PropertyUtil;
import org.extblog.wkm.web.vo.LeftItemFaceView;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

/**
 * @author WKM
 * FaceDao.java history
 * 1.WKM - Jan 15, 2013 - Create File
 */
@Component
public class FaceDao extends AbstractDao{
	
	/**
	 * 获取所有根节点
	 * @return
	 */
	public List<Map<String, String>> getRootItems(){
		
		log.info("query:" + PropertyUtil.getValue("items.select.roots"));
		
		return getTemplate().query(PropertyUtil.getValue("items.select.roots"),new Mapper());
	}
	
	/**
	 * 获取所有根节点
	 * @return
	 */
	public List<Map<String, String>> getRootItems(String itemId){
		
		log.info("query:" + PropertyUtil.getValue("items.select.item"));
		
		return getTemplate().query(PropertyUtil.getValue("items.select.item"),new Mapper(),itemId);
	}
	
	/**
	 * 根据父节点ID获取所有子节点
	 * @param parentId
	 * @return
	 */
	public List<Map<String, String>> getItemsByParentId(String parentId){
		log.info("query:" + PropertyUtil.getValue("items.select.children.byparentid"));
		return getTemplate().query(PropertyUtil.getValue("items.select.children.byparentid"),new Mapper(),parentId);
	}
	
	/**
	 * 根据D获取所有子节点
	 * @param parentId
	 * @return
	 */
	public List<Map<String, String>> getItems(){
		log.info("query:" + PropertyUtil.getValue("items.select.items"));
		return getTemplate().query(PropertyUtil.getValue("items.select.items"),new Mapper());
	}
	
	/**
	 * 添加数据
	 * @param map
	 * @return
	 */
	public int insertItem(Map<String, Object> map){
		log.info("insert:" + PropertyUtil.getValue("items.insert.items"));
		return getTemplate().update(PropertyUtil.getValue("items.insert.items"),
					map.get("itemId"),
					map.get("itemParent"),
					map.get("itemName"),
					map.get("itemType"),
					map.get("itemSeq"),
					map.get("itemDesc"));
	}
	
	public int updateItem(LeftItemFaceView view){
		log.info("update:" + PropertyUtil.getValue("items.update.items"));
		return getTemplate().update(PropertyUtil.getValue("items.update.items"),
					view.getItemName(),
					view.getItemDesc(),
					view.getItemId());
	}
	
	/**
	 * 更新数据
	 * @param map
	 * @return
	 */
	public int updateItem(Map<String, Object> map){
		log.info("insert:" + PropertyUtil.getValue("items.update.items"));
		return getTemplate().update(PropertyUtil.getValue("items.update.items"),
					map.get("itemParent"),
					map.get("itemName"),
					map.get("itemType"),
					map.get("itemSeq"),
					map.get("itemDesc"),
					map.get("itemId"));
	}
	
	public int deleteHomePage(String rootId){
		log.info("delete:" + PropertyUtil.getValue("items.delete.items.body"));
		int row = getTemplate().update(PropertyUtil.getValue("items.delete.items.body"),rootId);
		
		log.info("delete:" + PropertyUtil.getValue("items.delete.items.body2"));
		row = row + getTemplate().update(PropertyUtil.getValue("items.delete.items.body2"),rootId);
		
		log.info("delete:" + PropertyUtil.getValue("items.delete.items.child"));
		row = row + getTemplate().update(PropertyUtil.getValue("items.delete.items.child"),rootId);
		
		log.info("delete:" + PropertyUtil.getValue("items.delete.items.root"));
		row = row + getTemplate().update(PropertyUtil.getValue("items.delete.items.root"),rootId);
		
		return row;
	}
	
	/**
	 * 数据单条记录使用map记录
	 * @author WKM
	 * FaceDao.java history
	 * 1.WKM - Jan 16, 2013 - Create File
	 */
	private class Mapper implements RowMapper<Map<String, String>>{

		public Map<String, String> mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			// TODO Auto-generated method stub
			Map<String, String> map = new HashMap<String, String>();
			String itemId = rs.getString("item_id");
			if(itemId != null){
				map.put("itemId", itemId);
			}
			
			String itemParent = rs.getString("item_parent");
			if(itemParent != null){
				map.put("itemParent", itemParent);
			}
			
			String itemName = rs.getString("item_name");
			if(itemName != null){
				map.put("itemName", itemName);
			}
			
			map.put("itemType", String.valueOf(rs.getInt("item_type")));
			
			String itemSeq = rs.getString("item_seq");
			if(itemSeq != null){
				map.put("itemSeq", itemSeq);
			}
			
			String itemDesc = rs.getString("item_desc");
			if(itemDesc != null){
				map.put("itemDesc", itemDesc);
			}
			return map;
		}
	}
}
